#include <bits/stdc++.h>
using namespace std;
void build(vector<int>&a,int root,int length)
{

    int left = root<<1|1;
    int right = left+1;
    int min_index = right;
    if(min_index<length)
    {
        if(a.at(min_index)>a.at(left))
            min_index = left;
        if(a.at(min_index)<a.at(root))
        {
            swap(a.at(min_index),a.at(root));
            build(a,min_index,length);
        }

    }
}
void min_heap(vector<int>& a)
{
    for(int i=a.size()>>1;i>=0;i--)
        build(a,i,a.size());
}
void min_kth(vector<int>&a,int k)
{
    vector<int> heap_vec;
    int i=0;
    for(;i<k;i++)
        heap_vec.push_back(a[i]);
    min_heap(heap_vec);
    for(;i<a.size();i++) {
        if (a[i] > heap_vec[0])
        {
            heap_vec[0] = a[i];
            min_heap(heap_vec);
        }
    }
    cout<<"第"<<k<<"大的数为:"<<heap_vec[0]<<endl;
}
int main() {
    vector<int> a = {7,5,15,3,17,2,20,24,1,9,12,8};
    vector<int> b = {7,5,15,3,17};
    min_heap(b);
    min_kth(a,5);
    return 0;
}